package com.lee.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.CountDownLatch;

/**
 * @author lihaiqiang
 * @date 2022/1/24
 */
public class ZookeeperTest {

    public static void main(String[] args) throws Exception {
        CountDownLatch countDown = new CountDownLatch(1);
        Watcher watcher = watchedEvent -> {
            System.out.println("Type: " + watchedEvent.getType().name() + ", state: " + watchedEvent.getState().name());
            if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
                if (watchedEvent.getType() == Watcher.Event.EventType.None) {
                    countDown.countDown();
                } else if (watchedEvent.getType() == Watcher.Event.EventType.NodeCreated) {

                } else if (watchedEvent.getType() == Watcher.Event.EventType.NodeDeleted) {

                } else if (watchedEvent.getType() == Watcher.Event.EventType.NodeChildrenChanged) {

                } else if (watchedEvent.getType() == Watcher.Event.EventType.NodeDataChanged) {

                }
            }
        };
        String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
        ZooKeeper zooKeeper = new ZooKeeper(connectString, 5000, watcher);
        countDown.await();

        String basePath = "/lee";
        String childrenPath1 = basePath + "/name";
        String childrenPath2 = basePath + "/gender";

        // 创建节点
        String result;
        // 注册监听,每次都要重新注册，否则监听不到
        Stat stat = zooKeeper.exists(basePath, watcher);
        if (stat == null) {
            result = zooKeeper.create(basePath, "top".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("创建节点：" + result);
        }

        stat = zooKeeper.exists(childrenPath1, watcher);
        if (stat == null) {
            result = zooKeeper.create(childrenPath1, "name".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("创建节点：" + result);
        }

        stat = zooKeeper.exists(childrenPath2, watcher);
        if (stat == null) {
            result = zooKeeper.create(childrenPath2, "gender".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("创建节点：" + result);
        }

        // 获取节点
        byte[] data = zooKeeper.getData(basePath, true, null);
        result = new String(data);
        System.out.println("获取节点[" + basePath + "]数据：" + result);

        // 修改节点
        zooKeeper.setData(basePath, "parent".getBytes(), -1);
        System.out.println("设置节点[" + basePath + "]数据：parent");

        data = zooKeeper.getData(basePath, true, null);
        result = new String(data);
        System.out.println("获取节点[" + basePath + "]数据：" + result);

        // 删除节点（要先删除子节点）
        zooKeeper.delete(childrenPath1, -1);
        System.out.println("删除节点[" + childrenPath1 + "]成功");
        zooKeeper.delete(childrenPath2, -1);
        System.out.println("删除节点[" + childrenPath2 + "]成功");
        zooKeeper.delete(basePath, -1);
        System.out.println("删除节点[" + basePath + "]成功");
    }

}
